package com.crazy.web.service.user.impl;

import cn.hutool.core.util.StrUtil;
import com.crazy.model.custom.mapper.IUserRoleMapper;
import com.crazy.model.major.dto.Menu;
import com.crazy.model.major.entity.TUserLimit;
import com.crazy.model.major.entity.TUserRole;
import com.crazy.web.service.user.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/16 16:49
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private IUserRoleMapper userRoleMapper;

    @Override
    public Set<String> getUserRoleNames(Long userId) {
        List<TUserRole> tUserRoles = userRoleMapper.getRoleNameByUserId(userId);
        Set<String> result = new HashSet<>();
        for(TUserRole tUserRole : tUserRoles){
            if(StrUtil.isNotEmpty(tUserRole.getRequireRole())){
                result.add(tUserRole.getRequireRole());
            }
        }
        return result;
    }

    @Override
    public Set<String> getLimit(Long userId) {
        List<TUserLimit> tUserLimits = userRoleMapper.getLimitByUserId(userId);
        Set<String> result = new HashSet<>();
        for(TUserLimit tUserLimit : tUserLimits){
            if(StrUtil.isNotEmpty(tUserLimit.getRequireLimit())){
                result.add(tUserLimit.getRequireLimit());
            }
        }
        return result;
    }

    @Override
    public List<Menu> getMenus(Long userId) {
        List<TUserLimit> tUserLimits = userRoleMapper.getLimitByUserId(userId);
        List<Menu> allMenus = new ArrayList<>();
        //剔除非菜单的权限
        for(TUserLimit tUserLimit : tUserLimits){
            if(new Integer("1").equals(tUserLimit.getType())) {
                Menu menu = new Menu();
                menu.setId(tUserLimit.getId());
                menu.setParentId(tUserLimit.getParentId());
                menu.setMenuName(tUserLimit.getLimitName());
                menu.setUrl(tUserLimit.getUrl());
                menu.setOrderNo(tUserLimit.getOrderNo());
                allMenus.add(menu);
            }
        }
        List<Menu> topMenus = sortMenuById(null, allMenus);
        return sortMenu(topMenus, allMenus);
    }

    /**
     * 整理菜单
     * @param currMenus
     * @param allMenus 所有的菜单
     * @return
     */
    public List<Menu> sortMenu(List<Menu> currMenus, List<Menu> allMenus) {
        for(Menu menu : currMenus){
            List<Menu> subMenus = sortMenuById(menu.getId(), allMenus);
            if(subMenus!=null && !subMenus.isEmpty()){
                menu.setSubMenus(subMenus);
                sortMenu(subMenus, allMenus);
            }
        }
        return currMenus;
    }

    /**
     * 根据ID整理菜单
     * @param id
     * @param menus
     * @return
     */
    public List<Menu> sortMenuById(Long id, List<Menu> menus) {
        List<Menu> result = new ArrayList<>();
        Iterator<Menu> menuIterable = menus.iterator();
        while(menuIterable.hasNext()) {
            Menu menu = menuIterable.next();
            //最顶层
            if(id==null && menu.getParentId()==null){
                result.add(menu);
                menuIterable.remove();
                continue;
            }else if(id!=null && id.equals(menu.getParentId())) {
                result.add(menu);
                menuIterable.remove();
                continue;
            }
        }
        Collections.sort(result);
        return result;
    }

//    public static void main(String[] args) {
//        List<Menu> allMenus = new ArrayList<>();
//        Menu menu1 = new Menu(1L, null, "1");
//        Menu menu2 = new Menu(2L, null, "2");
//        Menu menu3 = new Menu(3L, null, "3");
//        Menu menu11 = new Menu(4L, 1L, "11");
//        Menu menu12 = new Menu(5L, 1L, "12");
//        Menu menu13 = new Menu(6L, 1L, "13");
//        Menu menu111 = new Menu(7L, 4L, "111");
//        Menu menu112 = new Menu(8L, 4L, "112");
//        Menu menu113 = new Menu(9L, 4L, "113");
//        Menu menu131 = new Menu(10L, 6L, "131");
//        Menu menu132 = new Menu(11L, 6L, "132");
//        Menu menu133 = new Menu(12L, 6L, "133");
//        Menu menu21 = new Menu(13L, 2L, "21");
//        Menu menu22 = new Menu(14L, 2L, "22");
//        Menu menu31 = new Menu(15L, 3L, "31");
//        Menu menu32 = new Menu(16L, 3L, "32");
//        Menu menu33 = new Menu(17L, 3L, "33");
//        Menu menu321 = new Menu(18L, 16L, "321");
//        Menu menu322 = new Menu(19L, 16L, "322");
//        Menu menu323 = new Menu(20L, 16L, "323");
//        allMenus.add(menu1);allMenus.add(menu2);allMenus.add(menu3);
//        allMenus.add(menu11);allMenus.add(menu12);allMenus.add(menu13);
//        allMenus.add(menu111);allMenus.add(menu112);allMenus.add(menu113);
//        allMenus.add(menu131);allMenus.add(menu132);allMenus.add(menu133);
//        allMenus.add(menu21);allMenus.add(menu22);allMenus.add(menu31);
//        allMenus.add(menu32);allMenus.add(menu33);allMenus.add(menu321);
//        allMenus.add(menu322);allMenus.add(menu323);
//        List<Menu> firstMenus = sortMenuById(null, allMenus);
//        List<Menu> result = sortMenu(firstMenus, allMenus);
//        System.out.println(result);
//    }
}
